package org.wendu.wdoa.security.home;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wendu.wdoa.common.CurrentUser;
import org.wendu.wdoa.common.Result;
import org.wendu.wdoa.common.WdoaConstants;
import org.wendu.wdoa.common.token.TokenUtils;
import org.wendu.wdoa.security.home.dto.MenuDto;

import java.util.List;

@RestController
@RequestMapping("/home")
public class HomeApi {

    @Autowired
    HomeService homeService;

    @GetMapping("/curr-user")
    public Result getCurrentUser(@RequestHeader(WdoaConstants.HEADER_NAME_TOKEN) String clientToken ){
        CurrentUser currentUser = TokenUtils.getCurrentUser(clientToken);
        return Result.OK(currentUser);
    }

    @GetMapping("/menu-list")
    public Result getMenuList(@RequestHeader(WdoaConstants.HEADER_NAME_TOKEN) String token){
        CurrentUser currentUser = TokenUtils.getCurrentUser(token);
        List<MenuDto> menuList = homeService.getMenuList(currentUser.getUserId());
        return Result.OK(menuList);
    }
}
